<template>
  <div class="auth-container">
    <!-- 左侧表单区 -->
    <div class="form-section">
      <div class="form-wrapper">
        <h2>用户登录</h2>
        <form @submit.prevent="handleLogin">
          <div class="form-group">
            <label for="login-username">用户名：</label>
            <input type="text" v-model="login.username" placeholder="请输入用户名" required>
          </div>
          <div class="form-group">
            <label for="login-password">密码：</label>
            <input type="password" v-model="login.password" placeholder="请输入密码" required>
          </div>
          <div class="form-group">
            <label for="login-identity">身份：</label>
            <select id="login-identity" v-model="login.identity">
              <option value="patient">普通用户</option>
              <option value="manager">管理人员</option>
            </select>
          </div>
          <button type="submit" class="login-btn">登 录</button>
        </form>
        <div class="footer-links">
          <a @click="switchToRegister">注册账号</a>
          <span>|</span>
          <a @click="switchToForgot">忘记密码</a>
        </div>
      </div>
    </div>

    <!-- 右侧展示区 -->
    <div class="display-section">
      <div class="form-wrapper">
        <div class="logo-container">
          <h3>欢迎使用“愈见希望”<br />
            病友交流平台</h3>
          <p>开启您的健康交流之旅</p>
          <img src="src\assets\images\logo.png" alt="网站Logo">
        </div>
        <div class="agreement">
          <p>登录即表示您同意我们的</p>
          <a href="./UserAgreement" target="_blank">《用户协议》</a>和
          <a href="./Privacy" target="_blank">《隐私政策》</a><br>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import axios from 'axios';

export default {
  data() {
    return {
      login: {
        username: '',
        password: '',
        identity: 'patient', // 默认身份为病友
      },
      errorMessage: '', // 用于显示错误信息
    };
  },
  methods: {
    async handleLogin() {
      this.errorMessage = ''; // 清空错误信息
      try {
        const response = await axios.post('http://localhost:8081/api/auth/login', {
          username: this.login.username,
          password: this.login.password,
          identity: this.login.identity,
        }, {
          withCredentials: true // 确保携带凭证
        });

        if (response.data.success) {
          const { identity } = response.data.data; // 假设后端返回的用户信息中包含 identity 字段
          if (identity === 'patient') {
            this.$router.push('/Index'); // 病友跳转到 /Index
          } else if (identity === 'manager') {
            this.$router.push('/home'); // 管理人员跳转到 /personals
          } else {
            this.errorMessage = '未知身份，请联系管理员';
          }
        } else {
          this.errorMessage = response.data.message || '登录失败，请检查用户名和密码';
          console.error('登录失败:', response.data.message || '未知错误');
        }
      } catch (error) {
        this.errorMessage = '网络错误，请稍后再试';
        console.error('登录失败:', error);
      }
    },
    switchToRegister() {
      this.$router.push('/Register');
    },
    switchToForgot() {
      this.$router.push('/ForgotPassword');
    }
  }
};
</script>

<style scoped>
html {
    lang: zh-CN;
}

.auth-container {
  width: 1000px;
  display: flex;
  min-height: 100vh;
  background: linear-gradient(135deg, #f5f9fc 0%, #e6f0fa 100%);
  margin: 0 auto;
  position: relative;
  padding: 0 50px; 
}

.form-section, .display-section {
  flex: 1;
  padding: 40px 0;
}

.auth-container::before {
  content: '';
  position: absolute;
  top: -50px;
  right: -50px;
  width: 200px;
  height: 200px;
  background: rgba(64, 158, 255, 0.08);
  border-radius: 50%;
}

.auth-container::after {
  content: '';
  position: absolute;
  bottom: -30px;
  left: -30px;
  width: 150px;
  height: 150px;
  background: rgba(64, 158, 255, 0.05);
  border-radius: 50%;
}

.form-section {
  flex: 0 0 480px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 1rem;
  position: relative;
  overflow: hidden;
}

.form-wrapper {
  width: 100%;
  max-width: 400px;
  background: white;
  border-radius: 16px;
  padding: 2rem;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.05);
  position: relative;
  z-index: 1;
  transition: transform 0.3s, box-shadow 0.3s;
}

.form-wrapper:hover {
  transform: translateY(-5px);
  box-shadow: 0 15px 40px rgba(0, 0, 0, 0.1);
}

h2 {
  text-align: center;
  margin-bottom: 30px;
  color: #333;
  font-size: 24px;
  font-weight: 500;
}

.form-group {
  display: flex; 
  align-items: center; 
  margin-bottom: 20px;
}
.form-group input,
.form-group select {
  flex: 1; /* 占据剩余空间 */
  padding: 12px 16px;
  border: 1px solid #e2e8f0;
  border-radius: 6px;
  font-size: 14px;
  transition: all 0.3s;
}

.form-group input:focus,
.form-group select:focus {
  border-color: #409eff;
  box-shadow: 0 0 0 2px rgba(64, 158, 255, 0.2);
  outline: none;
}

.form-group label {
  margin-bottom: 0; /* 移除原来的下边距 */
  margin-right: 10px; /* 添加右边距 */
  width: 80px; /* 设置固定宽度保持对齐 */
  text-align: right; /* 文本右对齐 */
  font-size: 14px;
  color: #5e6d82;
}
.form-group select {
  display: block;
  width: 100%;
  padding: 12px 15px;
  font-size: 14px;
  line-height: 1.5;
  color: #495057;
  background-color: #fff;
  background-clip: padding-box;
  border: 1px solid #ced4da;
  border-radius: 4px;
  transition: border-color 0.15s ease-in-out, 
              box-shadow 0.15s ease-in-out;
}

.login-btn {
  width: 100%;
  padding: 12px;
  background: #409eff;
  color: white;
  border: none;
  border-radius: 6px;
  font-size: 16px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.3s;
  margin-top: 10px;
}

.login-btn:hover {
  background: #66b1ff;
  box-shadow: 0 2px 8px rgba(103, 194, 58, 0.3);
}

.footer-links {
  margin-top: 24px;
  text-align: center;
  font-size: 14px;
  color: #909399;
}

.footer-links a {
  color: #409eff;
  cursor: pointer;
  text-decoration: none;
  transition: color 0.2s;
}

.footer-links a:hover {
  color: #66b1ff;
  text-decoration: underline;
}

.footer-links span {
  color: #dcdfe6;
  margin: 0 10px;
}

.display-section {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 1rem;
  position: relative;
  overflow: hidden;
}

.logo-container {
  text-align: center;
  margin-bottom: 2rem;
}

.logo-container img {
  width: 300px;
  height: 180px;
}

.logo-container h3 {
  font-size: 1.8rem;
  color: #2c3e50;
  font-weight: 600;
  margin-bottom: 0.8rem;
  position: relative;
  display: inline-block;
}

.logo-container h3::after {
  content: '';
  position: absolute;
  bottom: -8px;
  left: 50%;
  transform: translateX(-50%);
  width: 50px;
  height: 3px;
  background: linear-gradient(to right, #4facfe, #00f2fe);
}

.logo-container p {
  color: #718096;
  font-size: 1rem;
  line-height: 1.6;
  max-width: 80%;
  margin: 0 auto 1.5rem;
}

.agreement {
  margin-top: 2rem;
  text-align: center;
  font-size: 0.9rem;
  color: #718096;
  line-height: 1.6;
  position: relative;
  padding-top: 1.5rem;
}

.agreement::before {
  content: '';
  position: absolute;
  top: 0;
  left: 50%;
  transform: translateX(-50%);
  width: 80px;
  height: 1px;
  background: #e2e8f0;
}

.agreement a {
  color: #4facfe;
  font-weight: 500;
  text-decoration: none;
  transition: all 0.3s;
  position: relative;
}

.agreement a::after {
  content: '';
  position: absolute;
  bottom: -2px;
  left: 0;
  width: 0;
  height: 1px;
  background: #4facfe;
  transition: width 0.3s;
}

.agreement a:hover {
  color: #3a9cf7;
}

.agreement a:hover::after {
  width: 100%;
}

@media (max-width: 768px) {
  .display-section {
    padding: 1.5rem;
  }
  .logo-container img {
    width: 120px;
    height: 120px;
  }
  .logo-container h3 {
    font-size: 1.5rem;
  }
}
</style>